package jdbcmapper;
import lotus.domino.*;

import java.util.Date;
import java.util.Vector;

import com.robaone.dbase.types.DateType;
/**
 * <p>Title: Finances</p>
 * <p>Description: Program Logic for calculating charges</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class NotesDoc {
  protected Session m_session;
  protected Document m_doc;
  protected Vector m_subs;
  protected int index = 0;
  public NotesDoc(Session session,Document doc){
    this.m_session = session;
    this.m_doc = doc;
    this.m_subs = new Vector();
  }
  public Date retrieveDateTime(String fieldname){
    try{
      Vector dtarray = this.m_doc.getItemValueDateTimeArray(fieldname);
      if(dtarray.size() > 0){
        DateTime retval = (DateTime)dtarray.get(0);
        return retval.toJavaDate();
      }else{
        return null;
      }
    }catch(Exception e){
      try{
    	  String str = this.getString(fieldname);
    	  com.robaone.dbase.types.DateType dt = new DateType(str);
    	  return (java.util.Date)dt.getValue();
      }catch(Exception e2){
    	  return null;
      }
    }
  }
  public void setDateTime(String fieldname,Date value) throws Exception {
    DateTime dt = this.getSession().createDateTime(value);
    this.m_doc.replaceItemValue(fieldname,dt);
  }
  public Session getSession(){
    return this.m_session;
  }
  public Document getDocument(){
    return this.m_doc;
  }
  public Date getDate(String fieldname) throws Exception {
      return this.retrieveDateTime(fieldname);
  }
  public String getString(String fieldname) throws Exception {
      return this.getDocument().getItemValueString(fieldname);
  }
  public Integer getInteger(String fieldname) throws Exception {
      try{
          return new Integer(this.getDocument().getItemValueInteger(fieldname));
      }catch(Exception e){
          try{
              String str = this.getString(fieldname);
              return new Integer(str);
          }catch(Exception e1){
              return null;
          }
      }
  }
  public Double getDouble(String fieldname) throws Exception {
      try{
          double retval = this.getDocument().getItemValueDouble(fieldname);
          return new Double(retval);
      }catch(Exception e){
          return null;
      }
  }
  public boolean getBoolean(String fieldname) throws Exception {
      String value = this.getString(fieldname);
      if(value == null){
          return false;
      }
      if(value.length() == 0 || value.trim().toLowerCase().equals("no")){
          return false;
      }else{
          return true;
      }
  }
  public void save() throws Exception {
    this.getDocument().save(true,false);
  }
  protected Object getFirst(){
      this.index = 0;
      if(this.m_subs.size() < 1){
          return null;
      }else{
          return this.m_subs.get(0);
      }
  }
  protected Object getNext(){
      this.index++;
      if(this.m_subs.size() < this.index+1){
          return null;
      }else{
          return this.m_subs.get(this.index);
      }
  }
  public String getUID() throws Exception {
      return this.getDocument().getUniversalID();
  }
  public String getParentUID() throws NotesException {
	return this.getDocument().getParentDocumentUNID();
  }
}
